
abcd=int(input())
de=0
k=0
if abcd>999 and abcd<10000:
    a=abcd//1000
    b=abcd//100%10
    c=abcd//10%10
    d=abcd%1000
    q=abcd//100
    w=abcd//10
    e=abcd%1000//10
    r=abcd%1000
    t=abcd%100
    for i in range(2,a+1):
        if a%i==0:
            k+=1
    if k==1:
        de=max(de,a)
    k=0
    for i in range(2,b+1):
        if b%i==0:
           k+=1
    if k==1:
        de=max(de,b)
    k=0
    for i in range(2,c+1):
        if c%i==0:
           k+=1
    if k==1:
        de=max(de,c)
    k=0
    for i in range(2,d+1):
        if d%i==0:
           k+=1
    if k==1:
        de=max(de,d)
    k=0
    for i in range(2,q+1):
        if q%i==0:
           k+=1
    if k==1:
        de=max(de,q)
    k=0
    for i in range(2,w+1):
        if w%i==0:
           k+=1
    if k==1:
        de=max(de,w)
    k=0
    for i in range(2,e+1):
        if e%i==0:
           k+=1
    if k==1:
        de=max(de,e)
    k=0
    for i in range(2,r+1):
        if r%i==0:
           k+=1
    if k==1:
        de=max(de,r)
    k=0
    for i in range(2,t+1):
        if t%i==0:
           k+=1
    if k==1:
        de=max(de,t)
    k=0
    for i in range(2,abcd+1):
        if abcd%i==0:
           k+=1
    if k==1:
        de=max(de,abcd)
    print(de)
else:
    print("0")









